package com.app.cameraxmodule

import android.graphics.SurfaceTexture
import android.opengl.GLSurfaceView
import android.os.Bundle
import android.util.Log
import android.util.Size
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.*
import androidx.lifecycle.LifecycleOwner
import com.alibaba.android.arouter.facade.annotation.Route
import java.util.concurrent.TimeUnit
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

/**
 *  Create by zhouwd on 2020/03/11
 *  Describe:
 */
private val TAG = CameraXModuleActivity::class.java.simpleName

@Route(path = "/camerax_learn_module/camerax_learn_activity")
class CameraXModuleActivity : AppCompatActivity() , SurfaceTexture.OnFrameAvailableListener {

    override fun onFrameAvailable(surfaceTexture: SurfaceTexture?) {
        Log.i(TAG, "onFrameAvailable --> ")

        activityCameraXLearnGlsv?.requestRender()

    }

//    val config = ImageCaptureConfig.Builder()
//            .setLensFacing(...)
//            .setFlashMode(...)
//            .setTargetAspectRatio(...)
//            .build()
//    val previewConfig = PreviewConfig.Builder()
//            .setTargetRotation(windowManager.defaultDisplay.rotation)
//            .build()
//
//    val imageAnalysisConfig = ImageAnalysisConfig.Builder()
//            .setTargetResolution(Size(1280, 720))
//            .build()
//
//    val factory = SensorOrientedMeteringPointFactory(width, height)
//    val point = factory.createPoint(x, y)
//    val action = FocusMeteringAction.Builder.from(point,
//            FocusMeteringAction.MeteringMode.AF_ONLY)
//            .addPoint(point2, FocusMeteringAction.MeteringMode.AE_ONLY) // could have many
//            .setAutoFocusCallback { }
//            // auto calling cancelFocusAndMetering in 5 seconds
//            .setAutoCancelDuration(5, TimeUnit.SECONDS)
//            .build()

    private var activityCameraXLearnGlsv: GLSurfaceView? = null

    private var surfaceTexture: SurfaceTexture? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_camerax)

        activityCameraXLearnGlsv = findViewById(R.id.activity_camerax_glsv)

        activityCameraXLearnGlsv?.setEGLContextClientVersion(3)
        activityCameraXLearnGlsv?.setRenderer(render)
        /*渲染方式，RENDERMODE_WHEN_DIRTY表示被动渲染，只有在调用requestRender或者onResume等方法时才会进行渲染。RENDERMODE_CONTINUOUSLY表示持续渲染*/
        activityCameraXLearnGlsv?.renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY

        initCamera()
    }

    private fun initCamera() {

        val previewConfig = PreviewConfig.Builder()
                //.setImageInfoProcessor()
                .build()
        val preview = Preview(previewConfig)

        preview.setOnPreviewOutputUpdateListener {
            // Your code here. For example, use previewOutput?.getSurfaceTexture()
            // and post to a GL renderer.
            Log.i(TAG, "setOnPreviewOutputUpdateListener --> $it")
            surfaceTexture = it.surfaceTexture
            it.surfaceTexture.setOnFrameAvailableListener(this)

        }

        CameraX.bindToLifecycle(this as LifecycleOwner, preview)

    }

    private val render = object: GLSurfaceView.Renderer {

        override fun onDrawFrame(gl: GL10?) {
            Log.i(TAG, "onDrawFrame")

            //glStep()
        }

        override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
            Log.i(TAG, "onSurfaceChanged width -> $width -> height -> $height")
            //glResize(width, height)

        }

        override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
            Log.i(TAG, "onSurfaceCreated")
            //glInit(assets)

        }

    }



}